const app = getApp(),url = app.globalData.url;
Page({

  data: {
    showView:true,
    // 列表
    listdata:[],
    where:{
      pageNum:1,
      pageSize:10,
    },
    Nid:1,
    all_where:{
      pageNum:1,
      pageSize:10,
      user_uid:'',
    },
    search:{
      pageNum: 1, 
      pageSize: 10,
      companySize: "",
      enterpriseNature: "",
      industry: "",
      positionName: "",
      userUid: "",
      wageRange: "",
    }
  },

  onLoad: function (options) {

		// wx.setNavigationBarTitle({  title: xcxNmae})
  },
  reload() {
    let filterForm = wx.getStorageSync('work_search')
    filterForm.pageNum = 1
    this.setData({
      listdata: [],
      search: Object.assign({}, this.data.search, filterForm)
    })
    if(this.data.Nid == 2){
      this.list()
      return
    }
    this.getAllPosition()
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({ selected: 1 })
    }
    
    var that = this
    var userinfor = wx.getStorageSync('user_massage')
    that.data.all_where.user_uid = userinfor.userUid || ''
    if(!userinfor){
      wx.showModal({
        title: '提示',
        content: '你还未登录，是否前往登录？',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/HomePage/login/login',
            })
          } else if (res.cancel) {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        }
      })
    }
    that.setData({Nid:1})//在其他页面回来 重新赋值
    this.getAllPosition()
  },
  getAllPosition(){
    // let params = {
    //   companySize: "",
    //   enterpriseNature: "",
    //   industry: "",
    //   pageNum: this.data.search.pageNum,
    //   pageSize: this.data.search.pageSize,
    //   positionName: "",
    //   userUid: "",
    //   wageRange: "",
    // }
    wx.showLoading({ title: '加载中' })
    let _this = this
    wx.request({
      url: url + 'position/findWorkList',
      header:{ 'Content-Type': 'application/json',},
      data: this.data.search,
      method: "POST",
      success(res){
        wx.hideLoading()
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: resData.msg,
            icon:'error'
          })
          return
        }
        let listdata = (resData.data.content || []).filter(item => item.id) 
        
        if(_this.data.search.pageNum > 1 && listdata.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          listdata: [..._this.data.listdata, ...listdata]
        })
      },
      fail(){
        _this.hideLoading()
      }
    })

  },
  
  getHotPosition(){
    let params = {
      companySize: "",
      enterpriseNature: "",
      industry: "",
      pageNum: this.data.search.pageNum,
      pageSize: this.data.search.pageSize,
      positionName: "",
      userUid: "",
      wageRange: "",
    }
    wx.showLoading({ title: '加载中' })
    let _this = this
    wx.request({
      url: url + 'position/findWorkList',
      header:{ 'Content-Type': 'application/json',},
      data: params,
      method: "POST",
      success(res){
        wx.hideLoading()
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: resData.msg,
            icon:'error'
          })
          return
        }
        let listdata = (resData.data.content || []).filter(item => item.id) 
        
        if(_this.data.search.pageNum > 1 && listdata.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          listdata: [..._this.data.listdata, ...listdata]
        })
      },
      fail(){
        _this.hideLoading()
      }
    })
  },

  butType(e){
    let tid = e.currentTarget.dataset.id
    this.setData({Nid:tid,listdata:[]})
    if(tid == 2){
      this.data.where.pageNum = 1
      this.list()
    }else{
      this.data.where.pageNum = 1
      this.getAllPosition()
    }
  },
  list(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this// 列表
    // var uid = wx.getStorageSync('user_massage')
    // that.data.where.user_uid = uid.userUid || ''
    // that.data.where.user_uid = ''
    let where = that.data.where
    console.log(where)
    wx.request({
      // url:url+'position/getPositionXcxByPageInfoNew',//https://www.qugouzhigong.top/dgdx-Api/position/getPositionByPageInfo
      url:url+'position/getPositionByPageInfo',
      method: "GET",
      data:where,
      success:function(res){
				wx.hideLoading()
        console.log("热门",res);
        var ret = res.data
        if(ret.code == 200){
          that.setData({listdata:ret.data.content})//.content
        }else{
          wx.showToast({
            title: ret.msg,
            icon:'none'
          })
        }
      }
    })
  },
  //搜索
  search(){
    wx.navigateTo({
      url: '/HomePage/search/search',
    })
  },
  // 前往筛选 screen
  shaixuan(){
    wx.navigateTo({
      url: '/HomePage/screen/screen',
    })
  },
  // 详情
  detail(e){
    var that = this
    let did = e.currentTarget.dataset.id
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
    wx.navigateTo({
      url: '/HomePage/workdetail/workdetail?id='+did,
    })
  },
  /**   * 页面上拉触底事件的处理函数   */
  onReachBottom: function () {
    var that = this
    var Nid = that.data.Nid
    this.data.search.pageNum++
    this.setData({
      search: this.data.search
    })
    if(Nid == 2){//最新
      that.listpage()
    }else{//全部
      // that.allpage()
      this.getAllPosition()
    }
  },
  listpage(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    that.data.where.pageNum ++
    let where = that.data.where
    let listdata = that.data.listdata//数组合并用返回值
    // let arr2 = that.data.listdata
    // console.log('上拉分页加载',where)
    wx.request({
      url:url+'position/getPositionByPageInfo',
      method: "GET",
      data:where,
      success:function(res){
				wx.hideLoading()
        console.log("热门",res);
        var ret = res.data
        if(ret.code == 200){
          if(ret.data.content != ''){//.content
            listdata=listdata.concat(ret.data.content)//.content
            that.setData({listdata})
          }else{
            wx.showToast({
              title: '到底了~~',
              icon:'none'
            })
          }
        }
      }
    })
  },
      /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    that.data.where.pageNum = 1
    that.data.all_where.pageNum = 1
  },
})